//
//  YZMCardChooseView.m
//  YZMicroStock
//
//  Created by wangminhong on 16/7/12.
//  Copyright © 2016年 cqjr. All rights reserved.
//

#import "YZMCardChooseView.h"
#import "YZMBankCardModel.h"
#import "YZMUserService.h"

static const float card_choose_view_height_for_cell = 64.0;

@interface YZMCardChooseView()<UITableViewDataSource, UITableViewDelegate>

@end

@implementation YZMCardChooseView

{
    
    UITableView *_cardTV;
    NSArray *_cardArray;
    BOOL _addFlag;
}

- (id)init{
    
    self = [super init];
    
    if (self) {
        
        _cardArray = [NSArray array];
        _cardTV = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 1, 1) style:UITableViewStylePlain];
        _cardTV.delegate = self;
        _cardTV.dataSource = self;
        _cardTV.backgroundColor = [UIColor whiteColor];
        _cardTV.separatorColor = [YZMAppStyle colorForLine];
        _cardTV.separatorStyle = UITableViewCellSeparatorStyleNone;
        _cardTV.scrollEnabled = NO;
        
        self.titleLabel.text = @"选择我的银行卡";
    }
    
    return self;
}

- (void)setCardArray:(NSArray *)cardArray{
    
    if (!cardArray || cardArray.count == 0) {
        
        return;
    }
    
    self.selectedIndex = -1;
    
    NSMutableArray *temp = [NSMutableArray arrayWithArray:cardArray];
    
    if (temp.count < 3) {
        
        [temp addObject:@"添加银行卡"];
        _addFlag = YES;
        
    }else{
        
        _addFlag = NO;
    }
    
    _cardArray = temp;
    [_cardTV mas_updateConstraints:^(MASConstraintMaker *maker){
        
        maker.height.mas_equalTo(card_choose_view_height_for_cell *_cardArray.count);
    }];
    [_cardTV reloadData];
    
    [_cardTV removeFromSuperview];
    self.customView = _cardTV;
    
    [self show];
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *identifier = @"CardChooseCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifier];
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
        cell.textLabel.textColor = [YZMAppStyle colorForGrayText];
        cell.textLabel.font = [YZMAppStyle largeSizeFont];
        cell.detailTextLabel.textColor = [YZMAppStyle colorForGrayText];
        cell.detailTextLabel.font = [YZMAppStyle smallSizeFont];
    }
    
    if(indexPath.section == self.selectedIndex){
        
        cell.accessoryView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"icon_red_selected_card"]];
    }else{
        cell.accessoryView = nil;
    }
    
    if (indexPath.section == _cardArray.count -1) {
        
        if (_addFlag) {
            
            cell.textLabel.text = @"添加银行卡";
            cell.imageView.image = [UIImage imageNamed:@"icon_bg_plus_dis"];
            
            return cell;
        }
    }
    
    YZMBankCardModel *card = _cardArray[indexPath.section];
    cell.imageView.image = [card iconImage];
    cell.textLabel.text = card.bankName;
    cell.detailTextLabel.text = [card formatNumber];
    
    return cell;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return [_cardArray count];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return card_choose_view_height_for_cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (_addFlag && indexPath.section == _cardArray.count - 1) {
        
        if (self.addAction) {
            
            self.addAction();
        }
        [self dismissWithAnimationFlag:NO];
        return;
    }
    
    if (self.chooseAction) {
        
        self.chooseAction(indexPath.section);
    }
    [self dismissWithAnimationFlag:NO];
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return  1;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    UIView *line = [[UIView alloc] init];
    line.backgroundColor = [YZMAppStyle colorForLine];
    return line;
}


- (void)show{
    
    self.customView = _cardTV;
    [super show];
}

- (void)assignConstaints
{
    
    UIView *lastView = nil;
    
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_containerView).with.offset(15);
        make.centerX.equalTo(_containerView);
    }];
    
    self.contentLabel.hidden = YES;
    self.footLabel.hidden = YES;
    self.sureButton.hidden = YES;
    
    [self.customView mas_makeConstraints:^(MASConstraintMaker *maker){
        
        maker.top.equalTo(self.titleLabel.mas_bottom).offset(15);
        maker.left.equalTo(_containerView);
        maker.right.equalTo(_containerView);
        maker.centerX.equalTo(_containerView);
    }];
    
    {
        /*分割线*/
        UIView *line = [YZMAppStyle splitLine];
        [_containerView addSubview:line];
        [line mas_makeConstraints:^(MASConstraintMaker *maker){
            
            maker.left.right.equalTo(_containerView);
            maker.top.equalTo(self.customView.mas_bottom);
            maker.height.mas_equalTo(0.5);
        }];
        
        lastView = line;
    }
    
    if (!self.cancelButton.hidden) {
        
        [self.cancelButton mas_makeConstraints:^(MASConstraintMaker *maker){
            
            maker.top.equalTo(lastView.mas_bottom);
            maker.height.mas_equalTo(44);
            maker.width.equalTo(_containerView);
            maker.left.equalTo(_containerView);
        }];
        
        lastView = self.cancelButton;
    }
    
    [_containerView mas_makeConstraints:^(MASConstraintMaker *maker){
        
        maker.width.equalTo(self).offset(-80);
        maker.centerX.centerY.equalTo(self);
        maker.bottom.equalTo(lastView);
    }];
}

@end
